<?php

namespace app\api\controller\order;

use app\api\model\supplier\InvoiceOrderModel;
use app\common\model\supplier\InvoiceLog;
use app\Request;
use app\api\controller\Controller;
use app\api\model\supplier\InvoiceModel;
/**
 * 发票
 */
class InvoiceOrder extends Controller
{
    public function createData(Request $request)
    {
        $params = $request->param();
        $user = $this->getUser();
        $params['uid'] = $user['user_id'];
        $invoice = InvoiceOrderModel::listAll(['order_id' => $params['order_id'], 'uid' => $user['user_id']]);
        if (count($invoice) > 0) return $this->renderError('请勿重复申请');
        InvoiceOrderModel::createData($params);
        $om = new \app\common\model\order\Order();
        $order = $om->where(['order_id' => $params['order_id']])->find();
        $order['is_invoice'] = 1;
        $order->save();
        return $this->renderSuccess('操作成功');
    }

    public function listData(Request $request)
    {
        $user = $this->getUser();
        $params = $request->param();
        $params['uid'] = $user['user_id'];
        $list = InvoiceOrderModel::listAll($params, ['invoice', 'order']);
        return $this->renderSuccess('', $list);
    }

    /**
     *获取开票记录
     * getInvoiceLog
     * void
     * @return void
     * @Author kevin
     * @date 2024/3/25 17:31
     */
    public function getInvoiceLogData(Request $request){
        $user = $this->getUser();
        $params = $request->param();
        $map['order_id'] = $params['order_id'];
        $list = InvoiceLog::listAll($map);
        return $this->renderSuccess('开票记录',$list);
    }

    public function deleteData(Request $request)
    {
        $params = $request->param();
        $iom = InvoiceOrderModel::one($params);
        $om = new \app\common\model\order\Order();
        $order = $om->where(['order_id' => $iom['order_id']])->find();
        $order['is_invoice'] = 0;
        $order->save();
        InvoiceOrderModel::deleteData($params);
        return $this->renderSuccess('操作成功');
    }

    /**
     *发票详情
     * getDetail
     * @param Request $request
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/3/22 16:20
     */
    public function getDetail(Request $request){
        $params = $request->param();
        $order_id = $params['order_id'];
        $data = (new InvoiceOrderModel()) -> with(['invoice'])-> where('order_id',$order_id) -> find();
        return $this->renderSuccess('发票详情', $data);
    }
}
